Ternary expressions, while concise, can often lead to code that is difficult to read and understand, especially when they are nested or complex.
Prioritizing readability fosters maintainability and reduces the likelihood of bugs. Therefore, they should be removed in favor of more explicit
control structures, such as if
/else
statements, to improve the clarity and readability of the code.
Code examples
Noncompliant code example
System.out.println(i>10?"yes":"no"); // Noncompliant
Compliant solution
if (i > 10) {
System.out.println("yes");
} else {
System.out.println("no");
}